import 'package:flutter/material.dart';
import 'package:flutter_structure/app/theme/app_colors.dart';
import 'package:flutter_structure/app/theme/app_themes.dart';
import 'package:flutter_structure/app/theme/theme_controller.dart';
import 'package:flutter_structure/common/widgets/ripple_button.dart';
import 'package:flutter_structure/common/widgets/slide_animation.dart';
import 'package:get/get.dart';

class SettingTheme {
  static SlideAnimation? _animationInstance;
  static void show() {
    _animationInstance = SlideAnimation(
      content: _buildThemeContent(), // 动态内容
      position: 'center', // 弹窗居中显示
      dismissOnBackgroundTap: true, // 点击蒙层关闭
    );
    // 显示动画
    _animationInstance?.show();
  }


  static Widget _buildThemeContent() {
    Color? getBackgroundColor() {
      final themeMode = ThemeController.to.themeMode.value;
      if (themeMode == ThemeMode.light) {
        return AppThemes.lightTheme.extension<AppColors>()?.subjectPure!;
      } else if (themeMode == ThemeMode.dark) {
        return AppThemes.darkTheme.extension<AppColors>()?.subjectPure!;
      } else if (themeMode == ThemeMode.system) {
        final brightness = WidgetsBinding.instance.platformDispatcher.platformBrightness;
        if (brightness == Brightness.light) {
          return AppThemes.lightTheme.extension<AppColors>()?.subjectPure!;
        } else {
          return AppThemes.darkTheme.extension<AppColors>()?.subjectPure!;
        }
      }
      return AppThemes.lightTheme.extension<AppColors>()?.subjectPure!;
    }

    return Obx(
      ()=> Container(
        margin: const EdgeInsets.symmetric(horizontal: 20),
        decoration: BoxDecoration(
          color: getBackgroundColor(),
          borderRadius: BorderRadius.circular(12),
        ),
        padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildSwitchTile(),
            RippleButton(
              onTap: () {
                ThemeController.to.switchTheme(ThemeMode.light);
              },
              padding: const EdgeInsets.only(right: 4, top: 12, bottom: 12),
              child: Obx(
                ()=> Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                  const Text(
                      "浅色模式",
                      style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500),
                    ),
                    if (ThemeController.to.themeMode.value == ThemeMode.system) const Icon(Icons.block, size: 14 , color: Colors.orange,),
                    if (ThemeController.to.themeMode.value == ThemeMode.light) const Icon(Icons.check, size: 14, color: Colors.orange,),
                  ],
                
                )
              ),
            ),
            RippleButton(
              onTap: () {
                ThemeController.to.switchTheme(ThemeMode.dark);
              },
              padding: const EdgeInsets.only(right: 4, top: 12, bottom: 12),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                const Text(
                    "深色模式",
                    style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500),
                  ),
                  if (ThemeController.to.themeMode.value == ThemeMode.system) const Icon(Icons.block, size: 14, color: Colors.orange,),
                  if (ThemeController.to.themeMode.value == ThemeMode.dark) const Icon(Icons.check, size: 14, color: Colors.orange,),
                ],
              ),
            ),
          ],
        ),
    )
    );
  }

  static Widget _buildSwitchTile() {
    return Obx( ()=>
    Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          const Text(
            "跟随系统",
            style: TextStyle(fontSize: 13, fontWeight: FontWeight.w500),
          ),
          Transform.scale(
            scale: 0.5,
            child: Switch(
              value: ThemeController.to.themeMode.value == ThemeMode.system,
              onChanged: (value) {
                if (value) {
                  ThemeController.to.switchTheme(ThemeMode.system);
                } else {
                  ThemeController.to.switchTheme(ThemeMode.light);
                }
              },
              activeColor: Colors.orange,
            ),
          ),
        ],
      ),
    );
  }
}
